package hng.mapper;

import hng.dto.store.ProductQuery;
import hng.model.ProductDO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import hng.vo.ProductVO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 * 商品表 Mapper 接口
 * </p>
 *
 * @author 告白
 * @since 2024-06-27
 */
public interface ProductMapper extends BaseMapper<ProductDO> {

    /**
     * 查询首页3个分类的商品列表
     */
    @Select("select c.type, p.* " +
            "from index_product c " +
            "left join product p " +
            "on c.product_id = p.id " +
            "order by c.type asc")
    List<ProductVO> clientProduct();

    /**
     * 模糊查询搜索商品
     */
    @Select("select c.name as categoryName, p.* " +
            "from product p " +
            "left join product_category c " +
            "on p.category_id = c.id " +
            "where p.name like CONCAT('%', #{keyword}, '%') or c.name like CONCAT('%', #{keyword}, '%')")
    List<ProductVO> search(@Param("keyword") String keyword);

    /**
     * 查询指定数量的销量最高的产品
     */
    @Select("select * FROM product ORDER BY buy_num DESC LIMIT #{count}")
    List<ProductVO> getTopSellingProducts(@Param("count") int count);

    /**
     * 查询指定数量的id顺序的产品
     */
    @Select("select * FROM product ORDER BY id DESC LIMIT #{count}")
    List<ProductVO> getTopProducts(@Param("count") int count);

    /**
     * 商家分页查询商品
     */
    @Select("select * " +
            "from product " +
            "where store_id = #{storeId}")
    List<ProductDO> productByStore(@Param("storeId") Integer storeId);

    /**
     * 管理员分页查询商品
     */
    @Select("select * from product ")
    List<ProductDO> productByAdmin();

    /**
     * 商家通过商家id和商品id列表批量删除商品
     */
    @Delete({
            "<script>",
            "DELETE FROM product ",
            "WHERE id IN ",
            "<foreach item='id' collection='idList' open='(' separator=',' close=')'> ",
            "#{id} ",
            "</foreach> ",
            "AND store_id = #{storeId} ",
            "</script>"
    })
    void deleteBatchIdsAndShopId(@Param("idList") List<Integer> idList, @Param("storeId") Integer storeId);

    /**
     * 更新商品的销量
     */
    @Update("UPDATE product SET buy_num = buy_num + #{quantity} WHERE id = #{productId}")
    void updateProductSales(@Param("productId") int productId, @Param("quantity") int quantity);

}
